<?php

namespace App\Http\Controllers\manage;

use App\app;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Validator;

class appController extends Controller
{
    public function index(Request $request)
    {
        //当前页面标题
        $title = 'APP管理';
        $appModel = new app();
        //APP总数
        $count = $appModel->count();
        //APP信息
        $apps = $appModel->get();

        if ($request->method() == 'POST') {
            $messages = [
                'name.required' => '您没有填写 APP名称',
                'developer.required' => '您没有填写 开发商/运营商',
                'sourceID.required' => '您没有填写 内部源ID'
            ];

            $rules = [
                'name' => 'required',
                'developer' => 'required',
                'sourceID' => 'required'
            ];

            $validator = Validator::make($request->input(), $rules, $messages);
            $validator->validate();

            if (empty($AID = $request->input('AID'))) {
                //添加APP
                //保存文件到此本磁盘(方法返回路径)
                $path = $request->file('logo')->storeAs('logo', md5(time() . rand(0, 10000)));
                $appModel->name = $request->input('name');
                $appModel->text = $request->input('text');
                $appModel->developer = $request->input('developer');
                $appModel->sourceID = $request->input('sourceID');
                $appModel->updated_at = date("Y-m-d H:i:s");
                $appModel->logo = explode('/', $path)[1];
                $appModel->save();
                $validator->errors()->add('name', '已经添加APP');
                return back()->withErrors($validator);
            } else {
                //修改APP
                //保存文件到此本磁盘(方法返回路径)
                if ($request->file('logo') != null)
                    $path = $request->file('logo')->storeAs('logo', md5(time() . rand(0, 10000)));
                $app = $appModel->where('AID', $AID)->first();
                $app->name = $request->input('name');
                $app->text = $request->input('text');
                $app->developer = $request->input('developer');
                $app->sourceID = $request->input('sourceID');
                $app->updated_at = date("Y-m-d H:i:s");
                if ($request->file('logo') != null)
                    $app->logo = explode('/', $path)[1];
                $app->save();
                $validator->errors()->add('name', '成功保存修改！');
                return back()->withErrors($validator);
            }
        }
        return view("manage/app", compact('title', 'count', 'apps'));
    }

    //返回请求的logo
    public function logo(Request $request)
    {
        $headCode = $request->input('file');
        if (!Storage::exists("logo/$headCode")) {
            $headCode = 'default';
        }
        return Storage::get("logo/$headCode");
    }

    //删除APP
    public function appDel(Request $request)
    {
        $messages = [
            'AID.required' => '参数异常'
        ];

        $rules = [
            'AID' => 'required'
        ];

        $validator = Validator::make($request->input(), $rules, $messages);
        $validator->validate();

        $AID = $request->input('AID');
        $count = app::where('AID', $AID)->delete();
        if ($count == 1) {
            $validator->errors()->add('name', '已删除APP');
            return back()->withErrors($validator);
        } else {
            $validator->errors()->add('name', '删除APP失败');
            return back()->withErrors($validator);
        }
    }
}
